package easyway.trainapp.webframework.filter;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import easyway.train.privilege.domain.Petence;
import easyway.trainapp.webframework.util.BasicBackingBean;
import easyway.trainapp.webframework.util.FileUtil;

public class PopedomFilter extends BasicBackingBean implements Filter {

	/**zhangqihang
	 * 20100105
	 * Constructor of the object.
	 */

	private static final long serialVersionUID = 5162189625393315379L;
	// private static Logger LOG = Logger.getLogger(LoginFilter.class);
	/**
	 * 配置允许的角色
	 */
//	private List<Petence> petences=new ArrayList<Petence>();
	String username = null;
	String tp_userlevel = null;
	boolean flag = false;
	/**
	 * 重定向的URL
	 */
	private String redirectURl = null;
	private String welcomeURL = null;
	private String errorURL = null;

	private FilterConfig config;

	// private int i=1;

	/**
	 * 在过滤器中实现权限控制
	 */
	public void destroy() {

		this.config = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		res.addHeader("Cache-Control", "no-cache");
		res.setBufferSize(0);
		HttpSession session = req.getSession(false);
		if(session==null){
			session=req.getSession(true);
		}
		request.setCharacterEncoding("UTF-8");
		//////////////////////////////////
		
		String url = req.getServletPath();
		String path = req.getContextPath();
		String request_uri = req.getRequestURI();
		String ctx_path = req.getContextPath();
		String indexURL = url.substring(0, 6);
		FileUtil test=new FileUtil();
		flag=false;
		username = (String) session.getAttribute("username");
		tp_userlevel = (String)session.getAttribute("tp_userlevel");
//		petences = (List<Petence>) session.getAttribute("petences");
		if (username != null) {// 判断是否有用户登录，	如未登录返回登陆页面
		//	判断是否有权限,	如无权限返回欢迎页面提示无权限
//			if(petences!=null&&petences.size()>0){
//				Petence petence=new Petence();
//				for(int i=0;i<petences.size();i++){
//					petence=petences.get(i);
//					if(test.subFolder(url).contains("report")){//报表文件夹直接跳过，不需要判断，导师和管理员都有此权限，如需要修改到left_menu.xhtml页面去改判断就可以了
//						System.out.println(test.subFolder(url).contains("report"));
//						flag=true;
//						break;
//					}
//					if(test.subFolder(url).equals(test.subFolder(petence.getTp_path()))){//只要文件夹可用，就默认可用
//						flag=true;
//						break ;
//					}
//				}
//				if(flag){
//					chain.doFilter(request, response);
//				}else{
//					res.sendRedirect(errorURL);
//					return;
//				}
//			}else{
//				res.sendRedirect(welcomeURL);
//				return;
//			}
		} else {
//			res.sendRedirect(redirectURl);
			return;
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {

		config = filterConfig;
		// 指定要重定向的页面
		// welcomeURL = filterConfig.getInitParameter("welcome");
		String path = config.getServletContext().getContextPath();
		welcomeURL = path + "/home.faces";
		errorURL = path + "/error.faces";
		redirectURl = path + "/login.jsp";
	} 

	
	
	
}
